#include "pch.h"
#include "gui_cmd.h"
#include <cstdlib>
#include <string>
#include <iostream>
#include "solver.h"

using namespace std;

#pragma warning(disable : 4996)
#pragma warning(disable:4273)

char *argv[10];
const char *openFileError = "Open the error log error!";
int argc = 0;

char *call_by_cmd(int len, char *cmd) {

    char *st = cmd, *ed = cmd;
    FILE *logFile, *solFile;
    int r = freopen_s(&logFile, "error.log", "w", stderr);
    argc = 0;
    for (int i = 0; i < len; i++) {
        if (*ed == ' ') {
            int l = (int) (ed - st);
            argv[argc++] = (char *) malloc((size_t) l + 1);
            int j = 0;
            while (st != ed) {
                argv[argc - 1][j++] = *st++;
            }
            argv[argc - 1][j] = '\0';
            st = ed + 1;
        }
        ed++;
    }

    int l = (int) (ed - st);
    argv[argc++] = (char *) malloc((size_t) l + 1);
    char *tmp = argv[argc - 1];
    if (tmp) {
        while (st != ed) {
            *tmp++ = *st++;
        }
        *tmp = '\0';
    }

    r = freopen_s(&solFile, "solution.txt", "w", stdout);

    int ret = solve(argc, argv);

    fclose(logFile);

    r = freopen_s(&solFile, "CON", "w", stdout);

    char *res;
    if (ret < 0) {
        FILE *eFile;
        r = fopen_s(&eFile, "error.log", "r");
        if (eFile == 0) {
            res = (char *) malloc(26);
            if (res) {
                for (int i = 0; i < 26; i++)
                    res[i] = openFileError[i];
            }
            return res;
        }
        int c;
        string output = "";
        while ((c = fgetc(eFile)) != EOF) {
            output += c;
        }
        res = (char *) malloc(size_t(output.size() + 1));
        if (res) {
            tmp = res;
            for (int i = 0; i < output.size(); i++) {
                *tmp++ = output[i];
            }
            *tmp = '\0';
        }
        fclose(eFile);
    } else {
        res = (char *) malloc(1);
        if (res) {
            res[0] = '\0';
        }
    }

    return res;
}

//int main() {
//    char str[] = "D:\\new\\buaapaireprogramming\\cmake-build-release\\bin\\Wordlist.exe -h s -r -w D:\\new\\buaapaireprogramming\\input.txt";
//
//    char *res = call_by_cmd(113, str);
//    printf("%s\n", res);
//}
